package cn.tsvtc.shop.controller;/**
 * @Author Athena
 * @Date 2020/11/2 21:37
 * @Version 1.0
 * @Description no description
 */

import cn.tsvtc.shop.entity.ResultMessage;
import cn.tsvtc.shop.pojo.SearchProduct;
import cn.tsvtc.shop.service.SearchService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Athena
 * @date: 2020/11/2 21:37
 * @description:
 */
@Controller
@RequestMapping("search")
public class SearchController {

    private SearchService searchService;

    @Autowired
    public SearchController(SearchService searchService){
        this.searchService=searchService;
    }

    @GetMapping("import")
    @ResponseBody
    public ResultMessage initData(){
        return searchService.init();
    }

    @GetMapping("test/{keyword}")
    @ResponseBody
    public ResultMessage test(@PathVariable String keyword){
        List<SearchProduct> search = searchService.search(keyword);

        return new ResultMessage(true,"aaa",search);
    }

    @GetMapping("product/{keyword}")
    public String product(@PathVariable String keyword, Model model){
        List<SearchProduct> search = searchService.search(keyword);
        model.addAttribute("searchList",search);
        model.addAttribute("searchKeyword",keyword);
        model.addAttribute("num",search.size());
        return "search.html";
    }
}
